import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/norm_item.dart';
import 'package:p_location/common/widget/split_title_view.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/area_car_obj.dart';
import 'package:p_location/home/obj/guest_info_obj.dart';
import 'package:p_location/home/res/res_guest_detail.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_listener.dart';
import 'package:p_location/utils/string_util.dart';

class PageOuterCarDetail extends StatefulWidget {
  final int id;
  final String carNumber;
  final AreaCarObj carObj;

  PageOuterCarDetail(this.id, this.carNumber,{this.carObj});

  @override
  _PageGuestDetailState createState() => _PageGuestDetailState();
}

class _PageGuestDetailState extends State<PageOuterCarDetail>
    implements NetListener {
  GuestInfoObj _list;
  bool _isLoading = true;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    if(widget.carObj == null){
      _requestDetail();
    }else{
      _isLoading = false;
    }

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () {
                Navigator.pop(context);
                // 不写onPressed 默认，这个图片不能点击 且会有不可点击的样式（和 写了这个有不同的样式）
                // 如果有 onPressed 但是值是null 也会是不可点击的样式
              }),
          title: Text(widget.carNumber),
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
        ),
        body: _isLoading
            ? Center(
                child: CircularProgressIndicator(),
              )
            : Container(
                child: ListView(
                  children: [
                    SplitTitleView(
                      "车辆信息",
                      textColor: Color.fromRGBO(0, 0, 0, 0.65),
                      backgroundColor: Color.fromRGBO(239, 239, 243, 1),
                    ),
                    NormItem("车牌号", _getCarNum()),
                    NormItem("车辆类型", _getCarType()),
                    NormItem("运单号", _getBillNo()),
                    NormItem("押运员", _getYYType()),
                    NormItem("装载货物", _getLoadGoods()),
                    SplitTitleView(
                      "驾驶员信息",
                      textColor: Color.fromRGBO(0, 0, 0, 0.65),
                      backgroundColor: Color.fromRGBO(239, 239, 243, 1),
                    ),
                    NormItem("姓名", _getDriverName()),
                  ],
                ),
              ));
  }

  _getCarNum(){
    if(widget.carObj != null){
      return widget.carObj.carNum;
    }
    return _list.carNumber;
  }

  _getCarType(){
    if(widget.carObj != null){
      return widget.carObj.type;
    }
    return _list.carType;
  }

  _getBillNo(){
    if(widget.carObj != null){
      return widget.carObj.billNum;
    }
    return "-";
  }

  _getYYType(){
    if(widget.carObj != null){
      return widget.carObj.supercargo;
    }
    return _list.supercargo;
  }

  _getLoadGoods(){
    if(widget.carObj != null){
      return widget.carObj.loadGoodsName??'';
    }
    return "";
  }

  _getDriverName(){
    if(widget.carObj != null){
      return widget.carObj.driverName;
    }
    return _list.name;
  }

  _requestDetail() async {
    Map<String, dynamic> params = {};
    params["id"] = widget.id;
    await DioHttpClient.requestGet(
        context, HomeHttpUrl.GUEST_DETAIL_URL, params, this);
  }

  @override
  void onRequestSuccess(Response response) {
    if (response != null && response.data != null) {
      ResGuestDetail res = ResGuestDetail.fromJson(response.data);
      if (res != null && res.data != null) {
        _isLoading = false;
        if (res != null && res.data != null) {
          _list = res.data;
        }
        setState(() {});
      }
    }
  }

  @override
  void onRequestError(CommonRes errorRes) {}
}
